/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entities.Candidate;
import entities.Company;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.imageio.ImageIO;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author damfercer
 */
public class ImageStoreHelper {
    /*public static String pathToStoringFolder="/Users/damianfernandez/Recruitery/candidateImages/";		   
    public static String pathToTempDirectory="/Users/damianfernandez/Recruitery/videos/";
    public static String pathToPersistentStore="/Users/damianfernandez/Recruitery/candidateFiles/";	    
    public static String pathToCompanyPersistentStore="/Users/damianfernandez/Recruitery/companyFiles/";*/
    
    public static String pathToStoringFolder="/Recruitery/candidateImages/";
    public static String pathToTempDirectory="/Recruitery/videos/";
    public static String pathToPersistentStore="/Recruitery/candidateFiles/";
    public static String pathToCompanyPersistentStore="/Recruitery/companyFiles/";
    
    /*public static String pathToStoringFolder="C:\\Recruitery\\candidateImages\\";
    public static String pathToTempDirectory="C:\\Recruitery\\videos\\";
    public static String pathToPersistentStore="C:\\Recruitery\\candidateFiles\\";
    public static String pathToCompanyPersistentStore="C:\\Recruitery\\companyFiles\\";*/
    public static Integer maxPixels=640;
        
    public ImageStoreHelper(){
        
    }
    
    public static String storePrimeFacesBufferedImage(PrimeFacesBufferedImage pbi) throws IOException{
        Long mil = new Date().getTime();
        String imageName=pbi.getIdentifier().toString()+mil.toString()+".jpg";
        File f=new File(ImageStoreHelper.pathToStoringFolder+imageName);
        ImageIO.write(pbi.getBufferedImage(), "jpg",f);
        return imageName;
    }
    
    public static String storePersistentlyPrimeFacesBufferedImage(PrimeFacesBufferedImage pbi,Candidate c) throws IOException{
        Long mil = new Date().getTime();
        String imageName=pbi.getIdentifier().toString()+mil.toString()+".jpg";
        File f=new File(ImageStoreHelper.pathToPersistentStore+"/"+c.getIdCandidate().toString()+"/"+"images/"+imageName);
        ImageIO.write(pbi.getBufferedImage(), "jpg",f);
        return imageName;
    }
    
    public static String storePersistentlyPrimeFacesBufferedImage(PrimeFacesBufferedImage pbi,Company c) throws IOException{
        Long mil = new Date().getTime();
        String imageName=pbi.getIdentifier().toString()+mil.toString()+".jpg";
        File f=new File(ImageStoreHelper.pathToCompanyPersistentStore+"/"+c.getIdCompany().toString()+"/"+"images/"+imageName);
        ImageIO.write(pbi.getBufferedImage(), "jpg",f);
        return imageName;
    }
    
    
    public static PrimeFacesBufferedImage getPrimeFacesBufferedImageFromName(String s) throws IOException{
        File f=new File(ImageStoreHelper.pathToStoringFolder+s);
        BufferedImage read = ImageIO.read(f);
        PrimeFacesBufferedImage res=new PrimeFacesBufferedImage(read);
        return res;
    }
    /**
     * @return the pathToStoringFolder
     */
    public static String getPathToStoringFolder() {
        return pathToStoringFolder;
    }

    /**
     * @param aPathToStoringFolder the pathToStoringFolder to set
     */
    public static void setPathToStoringFolder(String aPathToStoringFolder) {
        pathToStoringFolder = aPathToStoringFolder;
    }
    
    public static String getPathToTemporaryDirectory(){
        return pathToTempDirectory;
    }
    
    public static void setPathToTemporaryDirectory(String tempPath){
        pathToTempDirectory=tempPath;
    }
    

    
    public static StreamedContent getVideoFromName(String name) throws FileNotFoundException{
        File f=new File(pathToTempDirectory+name);
        InputStream is=new FileInputStream(f);
        return new DefaultStreamedContent(is,"application/x-shockwave-flash");
    }

    
    public static void copyAndDelete(File origin, File destination) throws FileNotFoundException, IOException{
        if (!destination.exists()) {
            destination.createNewFile();
	}
        InputStream inStream = new FileInputStream(origin);
	OutputStream outStream = new FileOutputStream(destination);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inStream.read(buffer)) > 0){
            outStream.write(buffer, 0, length);
        }
        inStream.close();
        outStream.close();
        origin.delete();
    }
    
    
}
